package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayActivity;
import org.itboys.mobile.entity.mongo.SundayApply;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/7/12
 * @Description：
 */
@Service
public class SundayApplyService extends BaseMongoService<SundayApply> {
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }

    @Override
    protected Class<SundayApply> getEntityClass() {
        return SundayApply.class;
    }

    @Autowired
    private SundayActivityService activityService;

    /**
     * web端获取报名日志
     * @param request
     * @return
     */
    public PageResult<SundayApply> select(HttpServletRequest request) {
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name");
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request, "isProcessed");
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayApply> sundayApplyPageResult = super.containpageQuery(request, vagueParam, param);
        if (!ListUtil.isNotNull(sundayApplyPageResult.getData())) {
            return sundayApplyPageResult;
        }
        List<SundayApply> data = sundayApplyPageResult.getData();
        List<Long> ids=new ArrayList<>();//id集合
        for (SundayApply apply:data) {
            ids.add(apply.getActivityId());
        }
        //匹配活动名称
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in" ,ids);
        List<SundayActivity> activities = activityService.list(param);
        for (SundayApply apply:data) {
            for (SundayActivity activity:activities ) {
                if(apply.getActivityId()==activity.getId()){
                    apply.setActivityName(activity.getName());
                    break;
                }
            }
        }
        sundayApplyPageResult.setData(data);
        return sundayApplyPageResult;
    }

    //设置已处理报名
    public void processed(Long id){
        super.update(id,"isProcessed",SundayCommonConstants.YES);
    }
}